package com.wisdom.view;

import java.util.LinkedList;
import java.util.List;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.bluecall.sipua.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wisdom.tools.CCPConfig;
import com.wisdom.tools.HttpUtil;
import com.wisdom.view.adapter.HomePayAdapter;

public class HomePayActivity extends Activity implements OnClickListener{
	private ListView listView;
	private HomePayAdapter adapter;
	private Button newSms;
	private TextView money, title;
	private PayObject first_item;
	private static final String GET_PAY_URL = "http://www.1000call.com/api/mobile/callcards";
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home_sms_page);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
		init();
		new getDataAsyncTask().execute();
		
	}
	public void init(){


		listView = (ListView) findViewById(R.id.list);
		adapter = new HomePayAdapter(HomePayActivity.this);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(HomePayActivity.this, ShowWebActivity.class);
				intent.setAction(adapter.getItem(position).getUrl() + "?" + "userid=" + CCPConfig.UserId+ "&cardid=" + adapter.getItem(position).getCardId());
				HomePayActivity.this.startActivity(intent);
			}
		});
		money = (TextView)findViewById(R.id.money);
		title = (TextView)findViewById(R.id.title);
		
	}
	
	public class getDataAsyncTask extends AsyncTask<Void, Void, String>{

		@Override
		protected String doInBackground(Void... params) {
			return HttpUtil.postReturnStr(GET_PAY_URL, new JSONObject());
		}
		
		@Override
		protected void onPostExecute(String result) {
			List<PayObject> list_mmt = new Gson().fromJson(result, new TypeToken<List<PayObject>>(){}.getType());
			if(list_mmt.size() > 0){
				money.setText(list_mmt.get(0).getMoney() + "");
				title.setText(list_mmt.get(0).getTitle());
			}
			first_item = list_mmt.get(0);
			adapter.setData(list_mmt.subList(1, list_mmt.size()));
			adapter.notifyDataSetChanged();
			super.onPostExecute(result);
		}
		
	}

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.home_pay_first){
			if(first_item != null){
				Intent intent = new Intent(HomePayActivity.this, ShowWebActivity.class);
				intent.setAction(first_item.getUrl() + "?" + "userid=" + CCPConfig.UserId+ "&cardid=" + first_item.getCardId());
				HomePayActivity.this.startActivity(intent);
			}
		}
	}



}
